<?php

/**
 * Created by PhpStorm.
 * User: panya
 * Date: 2017/7/23
 * Time: 下午8:20
 */
class main extends CI_Controller
{
    function index()
    {

    }
    //发送海报
    function haibao($m)
    {
        $ls = explode('-', $m['haibao']);
        if ((mktime() - $ls[1]) < 259200) {
            $da['media_id'] = $ls[0];
            $da['type'] = 1;
            $this->send_zd_img($m['openid'], $da['media_id']);
        } else {
            //判断图片是否存在
            $img_url = './upfiles/member/' . $this->config->item('qrcode_pay') . '/' . $m['mid'] . '.jpg';
            if (!file_exists($img_url)) {
                $this->load->helper('string');
                //获取配置参数
                $q = $this->db->get_where('setting');
                $ls = $q->row_array(0);
                $qrcode = unserialize($ls['qrcode']);

                if ($m['type'] == 3) {
                    $formatMsg = 'shibai';
                    //获取背景图片
                    $groundImage = $qrcode['qr_bg'] ? $this->pub->oss_dz($qrcode['qr_bg'], FALSE) : './mex/lance/img/erwm1.jpg';
                    $ground_info = getimagesize($groundImage);
                    switch ($ground_info[2]) {
                        case 1 :
                            $ground_im = imagecreatefromgif($groundImage);
                            break;
                        case 2 :
                            $ground_im = imagecreatefromjpeg($groundImage);
                            break;
                        case 3 :
                            $ground_im = imagecreatefrompng($groundImage);
                            break;
                        default :
                            die($formatMsg);
                    }
                    //二维码处理
                    $erwm = $this->pub->get_ewm($m['mid'], 1);
                    $save_to = './upfiles/ls/' . random_string('unique') . '.jpg';
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_POST, 0);
                    curl_setopt($ch, CURLOPT_URL, $erwm);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $file_content = curl_exec($ch);
                    curl_close($ch);

                    $downloaded_file = fopen($save_to, 'w');
                    fwrite($downloaded_file, $file_content);
                    fclose($downloaded_file);

                    $erwm = $save_to;

                    $erwm_info = getimagesize($erwm);
                    switch ($erwm_info[2]) {
                        case 1 :
                            $erwm_im = imagecreatefromgif($erwm);
                            break;
                        case 2 :
                            $erwm_im = imagecreatefromjpeg($erwm);
                            break;
                        case 3 :
                            $erwm_im = imagecreatefrompng($erwm);
                            break;
                        default :
                            die($formatMsg);
                    }
                    @unlink($save_to);
                    //缩小二维码图片
                    $erwm_small = imagecreatetruecolor($qrcode['qr_k'], $qrcode['qr_g']);
                    imagecopyresampled($erwm_small, $erwm_im, 0, 0, 0, 0, $qrcode['qr_k'], $qrcode['qr_g'], $erwm_info['0'], $erwm_info['1']);
                    imagedestroy($erwm_im);

                    //把二维码加进去
                    imagecopyresampled($ground_im, $erwm_small, $qrcode['qr_left'], $qrcode['qr_top'], 0, 0, $qrcode['qr_k'], $qrcode['qr_g'], $qrcode['qr_k'], $qrcode['qr_g']);
                    imagedestroy($erwm_small);

                    //保存图片
                    imagejpeg($ground_im, $img_url);
                    //销毁资源
                    imagedestroy($ground_im);
                    $re_type = 1;

                    //上传文件到腾讯服务器
                    $token = $this->access_token();
                    $url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $token['access_token'] . '&type=image';
                    $back = $this->uploadMedia($url, $img_url);
                    $this->db->update('member', array('haibao' => $back->media_id . '-' . mktime()), array('mid' => $m['mid']));
                    $da['media_id'] = $back->media_id;
                    $this->send_zd_img($m['openid'], $da['media_id']);
                } else {
                    $re_type = 2;
                }
            } else {
                $re_type = 1;
                //上传文件到腾讯服务器
                $token = $this->access_token();
                $url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $token['access_token'] . '&type=image';
                $back = $this->uploadMedia($url, $img_url);
                $this->db->update('member', array('haibao' => $back->media_id . '-' . mktime()), array('mid' => $m['mid']));
                $da['media_id'] = $back->media_id;
                $this->send_zd_img($m['openid'], $da['media_id']);
            }
            if ($re_type == 1) {
                $da['url'] = site_url('upfiles/member/' . $this->config->item('qrcode_pay') . '/' . $m['mid'] . '.jpg');
            } else {
                $da['url'] = '';
            }
            $da['type'] = $re_type;
        }

        return $da;
    }

}

?>